//获取canvas上下文
const ctx = document.getElementById('canvas').getContext('2d');
const img = new Image();
img.src =
  'https://img0.baidu.com/it/u=3327425557,2750013450&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500';

img.onload = () => {
  //在canvas上绘制图片
  let pattern = ctx.createPattern(img, 'no-repeat');
  ctx.fillStyle = pattern;
  ctx.fillRect(0, 0, 500, 300);
};
